package com.lenovo.smarttraffic.wjy;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;

import java.io.Serializable;
import java.util.Hashtable;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class wjy_lxzs2 extends BaseActivity {

    private TextView QRtext;
    private ImageView QRcodeimage;

    String qrinform;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolBar(findViewById(R.id.toolbar),true,"支付界面");


        QRtext = (TextView) findViewById(R.id.QRtext);
        QRcodeimage = (ImageView) findViewById(R.id.QRcodeimage);
        QRtext.setVisibility(View.GONE);


        initdata();

        QRcodeimage.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                QRtext.setVisibility(View.VISIBLE);
                return false;
            }
        });

    }

    private void initdata() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://192.168.1.110:8088/transportservice/action/").addConverterFactory(GsonConverterFactory.create()).build();
        Interface_Sight interface_sight = retrofit.create(Interface_Sight.class);
        Call<Bean_Sightdata> user1 = interface_sight.getCall(new Bean_User1("user1"));
        user1.enqueue(new Callback<Bean_Sightdata>() {
            @Override
            public void onResponse(Call<Bean_Sightdata> call, Response<Bean_Sightdata> response) {
                if (response.code()==200){
                    List<Bean_Sightdata.ROWSDETAILBean> rows_detail = response.body().getROWS_DETAIL();
                    Intent intent = getIntent();
                    String sightname = rows_detail.get((Integer) intent.getSerializableExtra("sightId")).getName();
                    int sightprice = rows_detail.get((Integer) intent.getSerializableExtra("sightId")).getTicket();
                    qrinform="付款项目："+sightname+" "+"付款金额："+sightprice+"";
                    QRtext.setText(qrinform);
                    QRcodeimage.setImageBitmap(createQRCodeBitmap(qrinform,800,800,"UTF-8","H","1", Color.BLACK,Color.WHITE));


                }
            }

            @Override
            public void onFailure(Call<Bean_Sightdata> call, Throwable t) {
                Toast.makeText(wjy_lxzs2.this,"error",Toast.LENGTH_SHORT).show();
            }
        });

    }
    public static Bitmap createQRCodeBitmap(String content, int width, int height,
                                            String character_set, String error_correction_level,
                                            String margin, int color_black, int color_white) {
        if (TextUtils.isEmpty(content)){
            return null;
        }
        if (width<0||height<0){
            return null;
        }
        try {
            Hashtable<EncodeHintType,String> hints = new Hashtable<>();
            if (!TextUtils.isEmpty(character_set)){
                hints.put(EncodeHintType.CHARACTER_SET,character_set);
            }
            if (!TextUtils.isEmpty(error_correction_level)){
                hints.put(EncodeHintType.ERROR_CORRECTION,error_correction_level);
            }
            if (!TextUtils.isEmpty(margin)){
                hints.put(EncodeHintType.MARGIN,margin);
            }
            BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE,width,height,hints);
            int[] pixels = new int[width * height];
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    //bitMatrix.get(x,y)方法返回true是黑色色块，false是白色色块
                    if (bitMatrix.get(x, y)) {
                        pixels[y * width + x] = color_black;//黑色色块像素设置
                    } else {
                        pixels[y * width + x] = color_white;// 白色色块像素设置
                    }
                }
            }
            /** 4.创建Bitmap对象,根据像素数组设置Bitmap每个像素点的颜色值,并返回Bitmap对象 */
            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            return bitmap;
        }catch (WriterException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_wjy_lxzs2;
    }
}
